package tech.aistar.controller;

import tech.aistar.entity.Phone;
import tech.aistar.entity.PhoneType;
import tech.aistar.service.IPhoneService;
import tech.aistar.service.IPhoneTypeService;
import tech.aistar.service.impl.PhoneServiceImpl;
import tech.aistar.service.impl.PhoneTypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: lya
 * @Description: 本类的作用描述:
 * @CreateDate: 2019/9/5
 * @Version: 1.0
 */
@WebServlet("/phone/index")
public class PhoneIndexAction extends HttpServlet{
    private IPhoneService phoneService = new PhoneServiceImpl();
    private IPhoneTypeService phoneTypeService = new PhoneTypeServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接受查询的参数
        String name = req.getParameter("name");//根据名称迷糊查询
        //数据的回显
        req.setAttribute("name",name==null?"":name);

        //接收手机的类型
        String type_id = req.getParameter("type_id");//手机类型

        //用tid使type_id变为Integer类型
        Integer tid = null;
        if (type_id == null || "-1".equals(type_id)){
            tid = null;
        }else{
            tid = Integer.valueOf(type_id);
            //数据的回显
            req.setAttribute("type_id",tid);
        }

        //调用service查询手机信息
        List<Phone> phoneList = phoneService.findAll(name,tid);
        //放入到rquest作用域.
        req.setAttribute("phoneList",phoneList);


        //查询所有手机的类型
        List<PhoneType> phoneTypeList = phoneTypeService.findAll();
        req.setAttribute("phoneTypeList",phoneTypeList);


        //转发到view - servlet
        req.getRequestDispatcher("/nav/phone/indexView").forward(req,resp);


    }
}